package com.fit.pianosheetshop.pages;



import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;

import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.pianosheetshop.interfaces.User;
import com.fit.pianosheetshop.service.Users;
import com.fit.pianosheetshop.service.Security;

public class Login {
	private String userName;
	@Property
    private String password;
    
	// Za pristup "bazi" svih postojecih user-a
	@Inject
	@Property
	private Users users;

	// Za cuvanje logovanog user-a
	@SessionState
    private User user;

	Object onSubmitFromLoginForm() {
		System.out.println("In login form submit event handler!");
		Class nextPage = null;
		User authenticatedUser = Security.authenticate(userName, password, users);
		if (authenticatedUser != null) {
			// Autentikacija je uspjela
			// Zapisivanje logovanog user-a u session state object
			user = authenticatedUser;
			nextPage = Index.class;
		} else {
			// Autentikacija nije uspjela
			nextPage = Registration.class;

			
		}
		
		return nextPage;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
    
    

}
